﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.UpdatePlugin
{
    public partial class UpdatePage : Mimoza.GUI.WizardPage
    {
        UpdatePluginData m_data;
        Mimoza.ObjectLayer.OL m_objectLayer;

        class UpdateComboboxItem
        {
            public UpdateComboboxItem(Mimoza.ObjectLayer.Update update)
            {
                Update = update;
            }

            public override string ToString()
            {
                return Update.Name;
            }

            public Mimoza.ObjectLayer.Update Update;
        }

        public UpdatePage(Mimoza.ObjectLayer.OL objectLayer)
        {
            m_objectLayer = objectLayer;
            InitializeComponent();
        }

        override public void Init()
        {
            m_data = this.Wizard.UserData as UpdatePluginData;

            if (m_data.UpdateId != Guid.Empty)
            {
                comboBoxUpdate.Enabled = false; // can't edit created policy
            }

            foreach (Mimoza.ObjectLayer.Update update in m_objectLayer.UpdateRepository.GetUpdates())
            {
                comboBoxUpdate.Items.Add(new UpdateComboboxItem(update));
                if (update.ID == m_data.UpdateId)
                {
                    comboBoxUpdate.SelectedIndex = comboBoxUpdate.Items.Count - 1;
                }
            }

            comboBoxUpdate_SelectedIndexChanged(null, null);
            this.comboBoxUpdate.SelectedIndexChanged += new System.EventHandler(this.comboBoxUpdate_SelectedIndexChanged);
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            if (!CheckSelectedUpdate()) return false;

            m_data.Fill((comboBoxUpdate.SelectedItem as UpdateComboboxItem).Update);

            return true;
        }

        bool CheckSelectedUpdate()
        {
            if (comboBoxUpdate.SelectedItem==null)
            {
                MessageBox.Show(Resource.EmptyUpdateMessage, Resource.WrongParametersCaption,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                comboBoxUpdate.Focus();
                return false;
            }
            return true;
        }

        private void comboBoxUpdate_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateComboboxItem selectedItem = comboBoxUpdate.SelectedItem as UpdateComboboxItem;
            
            if (null == selectedItem)
            {
                return;
            }

            this.textBoxDescription.Text = selectedItem.Update.Description;
            this.textBoxOS.Text = selectedItem.Update.OS;
            this.textBoxPlatform.Text = selectedItem.Update.Model;
            this.textBoxVersion.Text = selectedItem.Update.Version;

            this.IsConfigured = true;

            if (null != sender)
            {
                this.RaiseOnPageModified();
            }
        }
    }
}
